package com.lemon.ai.socket.packets.spiritgame.model.dto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.lemon.ai.socket.packets.spiritgame.model.xml.XMLParser;

public class Table {
	
	private static final XMLParser XMLP = XMLParser.getInstance();
	
	private String name;
	private Column key = null;
	
	private List<Column> columns = new ArrayList<Column>();
	
	private String[] findByMethod;

	public String[] getFindByMethods () {
		return findByMethod;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Column> getColumns() {
		return columns;
	}

	/**
	 * 获得指定表的所有键, 如果XML中定义了指定的键集, 那么就返回指定的, 如果不指定, 那么就返回默认的键
	 * @param name
	 * @return
	 */
	public List<Column> getKeys() {
		List<Column> ls = new ArrayList<Column>();
		if(XMLP.contains(name)) {
			ls.add(getColumns(XMLP.getContact(name).key1));
			ls.add(getColumns(XMLP.getContact(name).key2));
		} else {
			if(key == null) {
				System.err.println("未找到主键, 暂时用第一列代替, tableName = " + getName());
				key = columns.get(0);
			}
			ls.add(key);
		}
		return ls;
	}
	
	/**
	 * 获得除主键外所有列
	 * @return
	 */
	public List<Column> getWithOutKeys() {
		
		List<Column> cs = new ArrayList<Column>();

		List<Column> keys = getKeys();
		for (Column c : columns) 
			if(!keys.contains(c)) 
				cs.add(c);
		
		return cs;
	}
	
	public void setKey(Column key) {
		this.key = key;
	}

	public Column getColumns(String name) {
		for (Column c : columns) {
			if(c.getName().equals(name)) {
				return c;
			}
		}
		return null;
	}

	/**
	 * 加入需要生成find方法的字段
	 * @param findByFields
	 */
	public void addFindByMethod(String... findByFields) {
		this.findByMethod = findByFields;
	}
	
	/**
	 * 获得所有相同的列
	 * @return
	 */
	public Map<String, List<Column>> getSameColumns() {
		Map<String, List<Column>> all = new HashMap<String, List<Column>>();
		for (Column c : columns) {
			String key = c.getName().replaceAll("[0-9]", "");
			List<Column> list = all.get(key);
			if(list == null) {
				list = new ArrayList<Column>();
				all.put(key, list);
			}
			list.add(c);
		}

		Iterator<Entry<String, List<Column>>> it = all.entrySet().iterator();
		
		while (it.hasNext()) {
			Entry<String, List<Column>> e = it.next();
			if(e.getValue().size() <= 1) {
				it.remove();
			}
		}
		return all;
	}

	/**
	 * 判断第i列是否属于一个组
	 * @param i
	 * @return
	 */
	public boolean isInGroup(int col) {
		Map<String, List<Column>> s = getSameColumns();
		Column c = this.getColumns().get(col);
		return s.containsKey(c.getName().replaceAll("[0-9]", ""));
	}
}
